package cn.xinfei.xdecision.common.service.sequence;

import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataException;
import cn.xinfei.xdecision.common.service.common.xyf.XyfReqData;
import cn.xinfei.xdecision.common.utils.util.HttpClient;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class SequenceSystem {
    @Resource
    private CommonConfig commonConfig;

    private static final Logger logger = LoggerFactory.getLogger(SequenceSystem.class);

    //	private static final String SEQUENCE_URL = commonConfig.getSequenceUrl();
    private static final String TYPE_RCS = "rcs";
    private static final String TYPE_SEQ = "seq";
    private static final String REQUEST_DATA = "request_data";
    private static final String REQUEST_BODY = "body";
    private static final String DEVICE_ID_SEQ = "device_id_seq";
    private static final String HEAD_SEQ = "head_sequence";
    /**
     * 前海序列号
     */
    private static final String QIANHAI_SEQNO = "qianhai_seqno";
    /**
     * 捷安序列号
     */
    private static final String JIEAN_SEQNO = "jiean_seqno";
    /**
     * 百度序列号
     */
    private static final String BAIDU_SEQNO = "baidu_seqno";
    /**
     * 新颜序列号
     */
    private static final String XINYAN_SEQNO = "xinyan_seqno";
    /**
     * 名单序列号
     */
    private static final String LIST_GROUP_ID = "list_group_id";

    /**
     * 华道序列号
     */
    private static final String HUADAO_SEQNO = "huadao_seqno";

    /**
     * 银融致信序列号
     */
    private static final String YINRONG_SEQNO = "yinrong_seqno";

    /**
     * 百行征信信序列号
     */
    private static final String BAIHANG_SEQNO = "baihang_seqno";
    /**
     * 银联智策序列号
     */
    public static final String ZHICE_SEQNO = "zhice_seqno";

    /**
     * 华策序列号
     */
    public static final String HUACE_SEQNO = "huace_seqno";

    /**
     * 黑瞳序列号
     */
    public static final String HICORE_SEQNO = "hicore_seqno";

    /**
     * 中原消金序列号
     */
    public static final String ZHONGYUAN_SEQNO = "zhongyuan_seqno";

    /**
     * 互金共享平台文件上报流水号
     */
    public static final String NIFA_SEQNO = "nifa_seqno";

    /**
     * 华融序列号
     */
    public static final String HUARONG_SEQNO = "huarong_seqno";
    /**
     * 海尔序列号
     */
    public static final String HARER_SEQNO = "haier_seqno";
    /**
     * AJ建序列号
     */
    public static final String AJ360_SEQNO = "aj360_seqno";
    /**
     * 鸿飞序列号
     */
    public static final String HONGFEI_SEQNO = "hongfei_seqno";
    /**
     * 银联序列号
     */
    public static final String CHINAUNION_SEQNO = "chinaunion_seqno";

    public static final String ZHIHUI_SEQNO = "zhihui_seqno";

    /**
     * 融360序列号
     */
    public static final String RONG360_SEQNO = "rong360_seqno";

    /**
     * 微众银行序列号
     */
    public static final String WEIZHONG_SEQNO = "weizhong_seqno";

    /**
     * 电话邦序列号
     */
    public static final String DIANHUABANG_SEQNO = "dianhuabang_seqno";
    /**
     * 华道佰鸪序列号
     */
    public static final String HUADAO_BIHOO_SEQNO = "huadaobihoo_seqno";


    /**
     * 航聚（百行）序列号
     */
    public static final String HANGJU_SEQNO = "hangju_seqno";

    /**
     * 互金运营商数据序列号
     */
    public static final String NIFADATA_SEQNO = "nifadata_seqno";

    /**
     * 中国联通
     */
    public static final String CUCC_SEQNO = "cucc_seqno";
    /**
     * 融汇风险分序列号
     */
    public static final String RONGHUI_SEQNO = "ronghui_seqno";

    public static final String ACCESS_CONTROL_SEQNO = "access_control_seqno";

    /**
     * 恒智普惠序列号
     */
    public static final String HENGZHI_SEQNO = "hengzhi_seqno";

    /**
     * 朴道序列号
     */
    public static final String PUDAO_SEQNO = "pudao_seqno";

    /**
     * 获取sequenceId
     *
     * @param name
     * @return
     */
    public long getNextSequenceId(String name, String type) throws XDecisionDataException {
        HttpClient httpClient = new HttpClient(commonConfig.getSequenceUrl());
        httpClient.setConnectTimeout(1000);
        httpClient.setReadTimeOut(1000);

        XyfReqData reqData = new XyfReqData();
        SequenceReqData sequenceReqData = new SequenceReqData(reqData);
        sequenceReqData.setName(name);
        sequenceReqData.setType(type);
        reqData.setHead(reqData.new SfyReqHead(false));
        reqData.setBody(sequenceReqData);
        SequenceRespData sequenceRespData = null;
        try {
            Map<String, String> reqMap = new HashMap<String, String>();
            reqMap.put(REQUEST_DATA, JSONObject.toJSONString(reqData));
            sequenceRespData = JSONObject.parseObject(JSONObject.parseObject(httpClient.post(reqMap)).getString(REQUEST_BODY), SequenceRespData.class);
            if (!sequenceRespData.getCode().equals("000000")) {
                logger.error("sequence 系统异常:", new RuntimeException(sequenceRespData.getCode()));
                return System.currentTimeMillis();
            }
        } catch (Exception e) {
            logger.error("sequence 系统异常:", e);
            return System.currentTimeMillis();
        }

        return sequenceRespData.getValue();
    }

    public Long getNextDeviceId() {
        return getNextSequenceId(DEVICE_ID_SEQ, TYPE_RCS);
    }

    public Long getNextHeadSeqId() {
        return getNextSequenceId(HEAD_SEQ, TYPE_SEQ);
    }

    public Long getQianhaiSeq() {
        return getNextSequenceId(QIANHAI_SEQNO, TYPE_RCS);
    }

    public Long getListGroupId() {
        return getNextSequenceId(LIST_GROUP_ID, TYPE_RCS);
    }

    public Long getJieanSeq() {
        return getNextSequenceId(JIEAN_SEQNO, TYPE_RCS);
    }

    public Long getBaidinSeq() {
        return getNextSequenceId(BAIDU_SEQNO, TYPE_RCS);
    }

    public Long getXinyanSeq() {
        return getNextSequenceId(XINYAN_SEQNO, TYPE_RCS);
    }

    public Long getHuadaoSeq() {
        return getNextSequenceId(HUADAO_SEQNO, TYPE_RCS);
    }

    public Long getYinRongSeq() {
        return getNextSequenceId(YINRONG_SEQNO, TYPE_RCS);
    }

    public Long getBaihangSeq() {
        return getNextSequenceId(BAIHANG_SEQNO, TYPE_RCS);
    }

    public Long getZhiceSeq() {
        return getNextSequenceId(ZHICE_SEQNO, TYPE_RCS);
    }

    public Long getHuaceSeq() {
        return getNextSequenceId(HUACE_SEQNO, TYPE_RCS);
    }

    public Long getHicoreSeq() {
        return getNextSequenceId(HICORE_SEQNO, TYPE_RCS);
    }

    public Long getZhongyuanSeq() {
        return getNextSequenceId(ZHONGYUAN_SEQNO, TYPE_RCS);
    }

    public Long getNifaSeq() {
        return getNextSequenceId(NIFA_SEQNO, TYPE_RCS);
    }

    public Long getHuaRongSeq() {
        return getNextSequenceId(HUARONG_SEQNO, TYPE_RCS);
    }

    public Long getHaierRongSeq() {
        return getNextSequenceId(HARER_SEQNO, TYPE_RCS);
    }

    public Long getAj360Seq() {
        return getNextSequenceId(AJ360_SEQNO, TYPE_RCS);
    }

    public Long getHongFeiSeq() {
        return getNextSequenceId(HONGFEI_SEQNO, TYPE_RCS);
    }

    public Long getRong360Seq() {
        return getNextSequenceId(RONG360_SEQNO, TYPE_RCS);
    }

    public Long getChinaUnionSeq() {
        return getNextSequenceId(CHINAUNION_SEQNO, TYPE_RCS);
    }

    public Long getZhihuiSeq() {
        return getNextSequenceId(ZHIHUI_SEQNO, TYPE_RCS);
    }

    public Long getWeizhongSeq() {
        return getNextSequenceId(WEIZHONG_SEQNO, TYPE_RCS);
    }

    public Long getDianhuabangSeq() {
        return getNextSequenceId(DIANHUABANG_SEQNO, TYPE_RCS);
    }

    public Long getHuadaoBihooSeq() {
        return getNextSequenceId(HUADAO_BIHOO_SEQNO, TYPE_RCS);
    }


    //
    public Long getHangjuSeq() {
        return getNextSequenceId(HANGJU_SEQNO, TYPE_RCS);
    }

    public Long getNifadataSeq() {
        return getNextSequenceId(NIFADATA_SEQNO, TYPE_RCS);
    }

    public Long getCuccSeq() {
        return getNextSequenceId(CUCC_SEQNO, TYPE_RCS);
    }

    public Long getRonghuiSeq() {
        return getNextSequenceId(RONGHUI_SEQNO, TYPE_RCS);
    }

    public Long getAccessControlSeq() {
        return getNextSequenceId(ACCESS_CONTROL_SEQNO, TYPE_RCS);
    }

    public Long getHengzhiSeq() {
        return getNextSequenceId(HENGZHI_SEQNO, TYPE_RCS);
    }

    public Long getPudaoSeq() {
        return getNextSequenceId(PUDAO_SEQNO, TYPE_RCS);
    }
}
